﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 
    /// </summary>
    public class AppTopicService : CrudAppService<AppTopic, AppTopicDto, Guid, AppTopicInput, AppTopicCreateDto, AppTopicUpdateDto>
    {
        /// <summary>
        /// 
        /// </summary>
        private IRepository<AppTopic, Guid> _topicConfigRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectConfigRepository"></param>
        public AppTopicService(IRepository<AppTopic, Guid> topicConfigRepository) : base(topicConfigRepository)
        {
            _topicConfigRepository = topicConfigRepository;
        }

        /// <summary>
        /// 获取消息业务类别
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<AppTopicDto>> GetListAsync(AppTopicInput input)
        {
            //if (input.OpenIddictApplicationId == null)
            //{
            //    return new PagedResultDto<AppTopicDto>();
            //}

            var iQuery = await _topicConfigRepository.GetQueryableAsync();
            //iQuery = iQuery.Where(i => i.OpenIddictApplicationId == input.OpenIddictApplicationId).OrderBy(o => o.Srtno);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<AppTopic>, List<AppTopicDto>>(iQuery.ToList());

                return new PagedResultDto<AppTopicDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<AppTopic>, List<AppTopicDto>>(items.ToList());

                return new PagedResultDto<AppTopicDto>(totalCount, itemsDto);
            }
        }
    }
}
